/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.demo.widget;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Fragment Wrapper<hr>
 * 默认Fragment的横屏实现和竖屏相同
 * @author Chaos	
 * @Description Wrapper
 * @date 2012-12-12 
 */
public abstract class BaseFragmentWrapper extends BaseFragment{
    
    private WidthMatching mWidthMatching;
    
    @Override
    protected View onCreateViewLand(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return onCreateViewPort(inflater, container, savedInstanceState);
    }
    
    @Override
    protected View onCreateViewUndefined(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return onCreateViewPort(inflater, container, savedInstanceState);
    }
    
    @Override
    protected void onOrientationChange(Orientation oldOri, Orientation newOri) {
        //do nothing
//        Utils.makeEventToast(getApplicationContext(), "从" + oldOri + "切换至" + newOri, false);
    }

    @Override
    protected void dealloc() {
        this.mWidthMatching = null;
        super.dealloc();
    }
    
    public WidthMatching getWidthMatching(){
        return getWidthMatchingIfNeed();
    }
    
    /**
     * 当屏幕方向改变时，获取预设匹配的宽度。
     */
    private WidthMatching findWidthMatching() {
        int width = (int) (getWidth() / getDensity());
        //修改匹配原则，改为向下匹配
        final int count = WidthMatching.values().length;
        
        for (int i = 0; i < count; i++) {
            WidthMatching match = WidthMatching.values()[i];
            if (match.getWidth() == width) {
                return match;
            }else if(match.getWidth() > width){
                return WidthMatching.values()[i == 0 ? 0 : i - 1];
            }
        }
        
        return findBestMatching(width);
    }
    
    private WidthMatching getWidthMatchingIfNeed(){
        if(mWidthMatching == null){
            mWidthMatching = findWidthMatching();
        }
        return mWidthMatching;
    }
    
    /**
     * 当屏幕宽度无法从匹配库中得到，则选取最优的返回。
     * <br>
     * 规则：如果屏幕过小，返回能匹配的最小的。
     *       如果屏幕过大，返回能匹配的最大的。
     */
    private WidthMatching findBestMatching(int width){
        if(width < WidthMatching.values()[0].getWidth()){
            return WidthMatching.values()[0];
        }else{
            return WidthMatching.values()[WidthMatching.values().length - 1];
        }
    }
    
    /**
     * 根据不同的宽度，展现不同的布局。
     * 这其中的枚举应该和drawable-wxxxdp定义的相同，以便对布局文件中的不足加以修正。
     * 当然你可以不使用此方法来进行修正，不过为了程序可以有更好的展现，应该调用此枚举来进行判断。
     * @author Chaos	
     * @Description 宽度匹配
     * @date 2013-1-7
     */
    public enum WidthMatching{
        //NOTICE:必须按照从小到大的顺序排列
        W320(320),
        W360(360),
        W533(533),
        W640(640);
        
        private int width;
        
        private WidthMatching(int width) {
            this.width = width;
        }
        
        public int getWidth(){
            return width;
        }
    }
    
}
